package cn.zhxu.pack;

import com.jfinal.kit.StrKit;

public class Config {

    public static final String E_PACK_SETTINGS = "e-pack.properties";

    private final String inputPath;
    private final String outputPath;
    private final String configPath;
    private final String[] binarys;       // 二进制文件后缀
    private final String[] ignores;
    private String incTrackPath;          // 增量追踪文件路径
    private String confExtName;
    private String theConfig;

    private static final Config config;

    private Config(String inputPath, String outputPath, String configPath,
                   String confExtName, String theConfig, String binarys, String ignores,
                   String incTrackPath) {
        this.inputPath = inputPath.trim();
        this.outputPath = outputPath.trim();
        this.configPath = configPath.trim();
        if (confExtName != null) {
            this.confExtName = confExtName.trim();
        }
        if (theConfig != null) {
            this.theConfig = theConfig.trim();
        }
        if (StrKit.isBlank(binarys)) {
            binarys = "png,jpg,ico,gif,font,ttf,woff,woff2,eot";
        }
        this.binarys = binarys.split(",");
        if (StrKit.isBlank(ignores)) {
            this.ignores = new String[0];
        } else {
            this.ignores = ignores.split(",");
        }
        if (incTrackPath != null) {
            this.incTrackPath = incTrackPath.trim();
        }
    }

    public static Config me() {
        return config;
    }

    public String getInputPath() {
        return inputPath;
    }

    public String getOutputPath() {
        return outputPath;
    }

    public String getConfigPath() {
        return configPath;
    }

    public String getConfExtName() {
        return confExtName;
    }

    public String getTheConfig() {
        return theConfig;
    }

    public String[] getBinarys() {
        return binarys;
    }

    public String[] getIgnores() {
        return ignores;
    }

    /**
     * @return 是否增量打包
     */
    public boolean isIncremental() {
        return StrKit.notBlank(this.incTrackPath);
    }

    public String getIncTrackPath() {
        return incTrackPath;
    }

    static {
        String inputPath = System.getProperty("e-pack.inputPath");
        String outputPath = System.getProperty("e-pack.outputPath");
        String configPath = System.getProperty("e-pack.configPath");
        String confExtName = System.getProperty("e-pack.confExtName");
        String theConfig = System.getProperty("e-pack.theConfig");
        String binarys = System.getProperty("e-pack.binarys");
        String ignores = System.getProperty("e-pack.ignores");
        String incTrackPath = System.getProperty("e-pack.incTrackPath");
        if (StrKit.isBlank(inputPath)) {
            System.err.println("inputPath is blank");
            System.exit(1);
        }
        if (StrKit.isBlank(outputPath)) {
            System.err.println("outputPath is blank");
            System.exit(1);
        }
        if (StrKit.isBlank(configPath)) {
            System.err.println("configPath is blank");
            System.exit(1);
        }
        if (StrKit.isBlank(confExtName)) {
            confExtName = ".properties";
        }
        config = new Config(inputPath, outputPath, configPath, confExtName,
                theConfig, binarys, ignores, incTrackPath);
    }

}